<template>
  <div class="dashboard">
    <!-- 侧边栏 -->
    <aside class="sidebar">
      <div class="sidebar-header">
        <h2>Easy Go</h2>
      </div>
      <nav class="sidebar-nav">
        <a href="#" class="nav-item active">
          <span class="nav-icon">📊</span>
          <span class="nav-text">数据概览</span>
        </a>
        <a href="#" class="nav-item">
          <span class="nav-icon">👥</span>
          <span class="nav-text">用户管理</span>
        </a>
        <a href="#" class="nav-item">
          <span class="nav-icon">📈</span>
          <span class="nav-text">统计分析</span>
        </a>
        <a href="#" class="nav-item">
          <span class="nav-icon">⚙️</span>
          <span class="nav-text">系统设置</span>
        </a>
        <a href="#" class="nav-item">
          <span class="nav-icon">🔔</span>
          <span class="nav-text">消息通知</span>
        </a>
      </nav>
    </aside>

    <!-- 主内容区 -->
    <main class="main-content">
      <!-- 顶部导航 -->
      <header class="top-header">
        <div class="header-left">
          <h1>数据概览</h1>
        </div>
        <div class="header-right">
          <div class="user-info">
            <div class="user-avatar">
              <img :src="userStore.currentUser?.avatar || '/default-avatar.png'" alt="用户头像">
            </div>
            <span class="username">{{ userStore.currentUser?.username }}</span>
            <button @click="logout" class="logout-btn">退出</button>
          </div>
        </div>
      </header>

      <!-- 数据卡片区域 -->
      <section class="stats-cards">
        <div class="stat-card yellow">
          <div class="stat-icon">💰</div>
          <div class="stat-content">
            <div class="stat-number">350</div>
            <div class="stat-label">总销售额</div>
          </div>
        </div>
        <div class="stat-card blue">
          <div class="stat-icon">👥</div>
          <div class="stat-content">
            <div class="stat-number">1,248</div>
            <div class="stat-label">用户数量</div>
          </div>
        </div>
        <div class="stat-card green">
          <div class="stat-icon">📊</div>
          <div class="stat-content">
            <div class="stat-number">89%</div>
            <div class="stat-label">完成率</div>
          </div>
        </div>
        <div class="stat-card purple">
          <div class="stat-icon">🔄</div>
          <div class="stat-content">
            <div class="stat-number">2,156</div>
            <div class="stat-label">访问量</div>
          </div>
        </div>
      </section>

      <!-- 图表区域 -->
      <section class="charts-section">
        <div class="chart-container">
          <div class="chart-header">
            <h3>销售趋势</h3>
            <select v-model="selectedPeriod" class="period-select">
              <option value="week">本周</option>
              <option value="month">本月</option>
              <option value="year">本年</option>
            </select>
          </div>
          <div class="chart-content">
            <!-- 折线图占位 -->
            <div class="chart-placeholder line-chart">
              <div class="chart-grid">
                <div v-for="i in 5" :key="i" class="grid-line"></div>
              </div>
              <div class="chart-line">
                <div v-for="(point, index) in lineChartData"
                     :key="index"
                     class="data-point"
                     :style="{ left: `${index * 20}%`, bottom: `${point}%` }">
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="chart-container">
          <div class="chart-header">
            <h3>用户活跃度</h3>
            <select v-model="selectedChartType" class="chart-type-select">
              <option value="bar">柱状图</option>
              <option value="line">折线图</option>
            </select>
          </div>
          <div class="chart-content">
            <!-- 柱状图占位 -->
            <div class="chart-placeholder bar-chart">
              <div class="chart-grid">
                <div v-for="i in 5" :key="i" class="grid-line"></div>
              </div>
              <div class="bars-container">
                <div v-for="(bar, index) in barChartData"
                     :key="index"
                     class="bar"
                     :style="{ height: `${bar}%` }">
                  <span class="bar-value">{{ bar }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const selectedPeriod = ref('week')
const selectedChartType = ref('bar')

// 模拟折线图数据
const lineChartData = computed(() => {
  const baseData = [20, 40, 60, 80, 60, 40, 20]
  return selectedPeriod.value === 'week' ? baseData :
      selectedPeriod.value === 'month' ? [10, 30, 50, 70, 90, 70, 50] :
          [5, 25, 45, 65, 85, 65, 45]
})

// 模拟柱状图数据
const barChartData = computed(() => {
  return [60, 80, 45, 70, 90, 50, 75]
})

const logout = () => {
  if (confirm('确定要退出登录吗？')) {
    userStore.logout()
    router.push('/')
  }
}

// 检查登录状态
onMounted(() => {
  if (!userStore.isLoggedIn) {
    router.push('/')
  }
})
</script>

<style scoped>
.dashboard {
  display: flex;
  min-height: 100vh;
  background-color: #f5f6fa;
}

/* 侧边栏样式 */
.sidebar {
  width: 250px;
  background: #1a1a1a;
  color: white;
  padding: 20px 0;
}

.sidebar-header {
  padding: 0 20px 20px;
  border-bottom: 1px solid #333;
  margin-bottom: 20px;
}

.sidebar-header h2 {
  margin: 0;
  color: #fff;
  font-size: 24px;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  color: #b0b0b0;
  text-decoration: none;
  transition: all 0.3s;
  border-left: 3px solid transparent;
}

.nav-item:hover, .nav-item.active {
  background: #2a2a2a;
  color: white;
  border-left-color: #007bff;
}

.nav-icon {
  margin-right: 10px;
  font-size: 18px;
}

.nav-text {
  font-weight: 500;
}

/* 主内容区样式 */
.main-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.top-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  background: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.header-left h1 {
  margin: 0;
  color: #333;
  font-size: 24px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #007bff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.username {
  font-weight: 500;
  color: #333;
}

.logout-btn {
  padding: 8px 16px;
  background: #ff4757;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
}

.logout-btn:hover {
  background: #ff3742;
}

/* 数据卡片样式 */
.stats-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 25px;
  border-radius: 10px;
  color: white;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.stat-card.yellow { background: linear-gradient(135deg, #f39c12, #f1c40f); }
.stat-card.blue { background: linear-gradient(135deg, #3498db, #2980b9); }
.stat-card.green { background: linear-gradient(135deg, #27ae60, #2ecc71); }
.stat-card.purple { background: linear-gradient(135deg, #9b59b6, #8e44ad); }

.stat-icon {
  font-size: 40px;
  margin-right: 15px;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
}

/* 图表区域样式 */
.charts-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.chart-container {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.chart-header h3 {
  margin: 0;
  color: #333;
}

.period-select, .chart-type-select {
  padding: 5px 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.chart-content {
  height: 300px;
  position: relative;
}

.chart-placeholder {
  width: 100%;
  height: 100%;
  background: #f8f9fa;
  border-radius: 5px;
  position: relative;
  overflow: hidden;
}

/* 折线图样式 */
.line-chart {
  position: relative;
}

.chart-grid {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.grid-line {
  border-top: 1px solid #e0e0e0;
}

.chart-line {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.data-point {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #3498db;
  border-radius: 50%;
  transform: translate(-50%, 50%);
}

.data-point::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  background: #3498db22;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* 柱状图样式 */
.bar-chart {
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  padding: 20px;
}

.bars-container {
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  width: 100%;
  height: 100%;
}

.bar {
  width: 30px;
  background: #3498db;
  border-radius: 3px 3px 0 0;
  position: relative;
  transition: height 0.3s;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.bar-value {
  position: absolute;
  top: -25px;
  font-size: 12px;
  font-weight: bold;
  color: #333;
}
</style>